package mysale.dao.impl;

import mysale.dao.FoodDao;
import mysale.pojo.Food;
import mysale.pojo.User;
import mysale.service.FoodService;
import mysale.service.impl.FoodServiceImpl;
import mysale.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SnakeWoodMan
 * @create 2020-12-24 15:10
 */
public class FoodDaoImpl implements FoodDao {
    private Connection con;
    public FoodDaoImpl(Connection con){this.con = con;}

    @Override
    public List<Food> getAllfoodinfo() {
        String sql = "select * from food";
        con = JDBCUtil.getConnection();
        List<Food> foodList = new ArrayList<Food>();
        PreparedStatement pstmt = null;
        ResultSet rs =null;
        Food food = null;
        try {
            pstmt = con.prepareStatement(sql);
            rs =  pstmt.executeQuery();//rs存放sql查询结果
            while(rs.next()){
                food = new Food();
                food.setFid(rs.getInt("fid"));
                food.setPic(rs.getString("pic"));
                food.setFtid(rs.getInt("ftid"));
                food.setFoodname(rs.getString("foodname"));
                food.setFoodinfo(rs.getString("foodinfo"));
                food.setPrice(rs.getFloat("price"));
                food.setUnit(rs.getString("unit"));
                food.setStock(rs.getInt("stock"));
                food.setFlavor(rs.getString("flavor"));
                foodList.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
            try {
                pstmt.close();
            } catch (SQLException e){
                e.printStackTrace();
            }

        }
        return foodList;
    }

    @Override
    public List<Food> selectLikeFoodname(String foodname) {
        String sql = "select * from food where foodname like ? or foodinfo like ?";
        con = JDBCUtil.getConnection();
        List<Food> foodList = new ArrayList<Food>();
        PreparedStatement pstmt = null;
        ResultSet rs =null;
        Food food = null;
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, "%"+foodname+"%");
            pstmt.setString(2, "%"+foodname+"%");
            rs =  pstmt.executeQuery();//rs存放sql查询结果
            while (rs.next()){
                food = new Food();
                food.setFid(rs.getInt("fid"));
                food.setPic(rs.getString("pic"));
                food.setFtid(rs.getInt("ftid"));
                food.setFoodname(rs.getString("foodname"));
                food.setFoodinfo(rs.getString("foodinfo"));
                food.setPrice(rs.getFloat("price"));
                food.setUnit(rs.getString("unit"));
                food.setStock(rs.getInt("stock"));
                food.setFlavor(rs.getString("flavor"));
                foodList.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try{
                rs.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
            try {
                pstmt.close();
            } catch (SQLException e){
                e.printStackTrace();
            }

        }
        return foodList;
    }

    @Override
    public List<Food> selectbyFtid(String ftid) {
        String sql = "SELECT " +
                "food.fid,food.foodname,food.pic,food.foodinfo," +
                "food.flavor,food.price,food.stock,food.unit," +
                "foodtype.ftid " +
                "FROM foodtype,food " +
                "WHERE foodtype.ftid= ? AND " +
                "foodtype.ftid = food.ftid";
        con = JDBCUtil.getConnection();
        List<Food> foodList = new ArrayList<Food>();
        PreparedStatement pstmt = null;
        ResultSet rs =null;
        Food food = null;
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, ftid);
            rs =  pstmt.executeQuery();//rs存放sql查询结果
            while (rs.next()){
                food = new Food();
                food.setFid(rs.getInt("fid"));
                food.setPic(rs.getString("pic"));
                food.setFtid(rs.getInt("ftid"));
                food.setFoodname(rs.getString("foodname"));
                food.setFoodinfo(rs.getString("foodinfo"));
                food.setPrice(rs.getFloat("price"));
                food.setUnit(rs.getString("unit"));
                food.setStock(rs.getInt("stock"));
                food.setFlavor(rs.getString("flavor"));
                foodList.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try{
                rs.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
            try {
                pstmt.close();
            } catch (SQLException e){
                e.printStackTrace();
            }

        }
        return foodList;
    }
}
